#!/usr/bin/python
# -*- coding: utf-8 -*-

from BeautifulSoup import BeautifulSoup
import urllib
import re
import time
import os

def getDate():
    return unicode(time.strftime( "%Y-%m-%d",time.localtime(time.time())))

def write2db(filename,entry):
    hdb = open(filename,'a')
    hdb.write(entry.encode('utf-8'))
    hdb.close()

def writeHeader2db(filename,header):
    if(os.path.isfile(filename)):
        return
    else:
        #print header
        write2db(filename,header)
        return



def getEntryStr(key,valuelist):
    '''
    return the entry string, TODO: unicode convert to string?
    '''
    #print key
    #print valuelist
    retstr = "" + key
    for i in valuelist:
        retstr = retstr + "\t" + i
    #print retstr
    return retstr+ "\n"

def main():
    url="http://www.hzfc365.com"
    filename= '/home/zhenghwa/analysis_dir/information.db'
    tSuiteTail = u'套'
    tAreaTail = u'm2'
    tDate = u'日期'
    tAvaliableNew = '今日商品房可售信息'.decode('utf-8')
    tSoldNewInfo = u'商品房累计成交信息'
    tSold2handInfo = u'二手房累计成交信息'
    tSoldNewNum = u'成交套数'
    tSoldNewArea= u'成交面积'
    tSoldResNewNum = u'其中住宅成交套数'
    tSoldResNewArea = u'其中住宅成交面积'
    tAvaAllNum = u'可售套数'
    tAvaAllArea = u'可售面积'
    tAvaAllResNum = u'其中住宅可售套数'
    tAvaAllResArea = u'其中住宅可售面积'
    tSold2handResNum = u'二手住宅套数'
    tSold2handResArea = u'二手住宅面积'
    tSold2handOtherNum = u'二手非住宅套数'
    tSold2handOtherArea = u'二手非住宅面积'

    tHeaderList =[ tAvaAllNum,tAvaAllArea,tAvaAllResNum,tAvaAllResArea,tSoldNewNum,tSoldNewArea, tSoldResNewNum, tSoldResNewArea,tSold2handResNum,tSold2handResArea,tSold2handOtherNum,tSold2handOtherArea   ]

    html_src = urllib.urlopen(url).read()
    mysoup = BeautifulSoup(html_src,fromEncoding="gbk")
    #print type(mysoup.title.string)
    #print mysoup.findAll('dt')

    dResultDict={}

    for item in mysoup.findAll('dt'):
        #print item
        t = item.getText()
        if t.__contains__(tAvaliableNew):
            tabAvailableNew = item.parent
        elif t.__contains__(tSoldNewInfo):
            tabSoldNewInfo = item.parent
        elif t.__contains__(tSold2handInfo):
            tabSold2handInfo = item.parent
        else:
            continue
    ls=[]
    for item in tabAvailableNew.findAll('td'):
        t = item.getText()
        ls.append(t)

    for i in range(0,4):
        dResultDict[ls[i]]=ls[i+4]
     
    ls=[]
    for item in tabSoldNewInfo.findAll('td'):
        t = item.getText()
        ls.append(t)

    for i in range(1,5):
       dResultDict[ls[i]]=ls[i+5]

    ls=[]
    for item in tabSold2handInfo.findAll('td'):
        t = item.getText()
        ls.append(t)

    preuText = u'二手'
    for i in range(3,7):
        if i <5:
                dResultDict[preuText+ls[1]+ls[i]]=ls[i+5]
        else:
            dResultDict[preuText+ls[2]+ls[i]]=ls[i+5]


    resultlist = []
    for i in tHeaderList:
        resultlist.append(dResultDict[i].replace(tAreaTail,tSuiteTail).rstrip(tSuiteTail))

    dbdict = {}
    dbdict[getDate()] = resultlist

    bbb = getEntryStr(getDate(),resultlist)
    ccc = getEntryStr(tDate,tHeaderList)
    writeHeader2db(filename,ccc);
    write2db(filename,bbb)

if __name__ == '__main__':
    main()


